package test.multi_thread.synch_674;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import log.debug.LogInterface;
import log.debug.LogTool;

public class EventChecker implements Runnable{
	private IntGenerator generator;
	private final int id;
	
	
	private static final LogInterface mLog = new LogTool().getLogType();
	private static final String TAG = "EventChecker";
	

	public EventChecker(IntGenerator generator, int id) {
		super();
		this.generator = generator;
		this.id = id;
	}


	public void run() {
		// TODO Auto-generated method stub
		int val = 0;
		while (!generator.isCanceled()){
			val = generator.next();
			if (val%2 != 0){
				mLog.i(TAG, val + " not even, threadInfo=" + Thread.currentThread().getName());
				generator.cancel();
			}
		}
		mLog.i(TAG, "run over, threadInfo=" + Thread.currentThread().getName() + "\t val=" + val);
	}
	
	public static void  test(IntGenerator gp, int count) {
		mLog.i(TAG, "start test " + count + "  thread") ;
		ExecutorService exec = Executors.newCachedThreadPool();
		for (int i=0; i<count; i++){
			exec.execute(new EventChecker(gp, i));
		}
		exec.shutdown();
	}
	
	public static void  test(IntGenerator gp) {
		test(gp, 5);
	}

}
